package com.lvmama.dashboard.dao.search.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;

import com.lvmama.com.lvtu.common.utils.DateUtil;
import com.lvmama.dashboard.dao.search.ILogSearchDao;
import com.lvmama.dashboard.model.page.PageResult;
import com.lvmama.dashboard.model.rop.search.request.LogSearchRequest;
import com.lvmama.java.rhino.spark.tables.ElasticLogBaseParam;
import com.lvmama.java.rhino.spark.utils.Constants;
import com.lvmama.java.rhino.spark.utils.ElasticUtils;
import com.lvmama.java.rhino.spark.utils.ElasticUtils.ElasticPageResult;
import com.lvmama.java.rhino.spark.utils.ElasticUtils.ElasticSearchResult;

public class LogSearchDaoImpl implements ILogSearchDao {
	
	private static final Logger LOGGER = Logger.getLogger(LogSearchDaoImpl.class);
	private static final String elasticIndex = Constants.ELASTIC_INDEX_LVTU_INDEX;
	@Override
	public PageResult<ElasticSearchResult<ElasticLogBaseParam>> queryLog(LogSearchRequest request) {
		BoolQueryBuilder filter = getFilter(request);
		LOGGER.debug(filter.toString());
		BoolQueryBuilder query = getQuery(request);
		LOGGER.debug(query.toString());
		int page = request.getPage();
		int pageRows = request.getPageRows();
		int startRows = (page - 1) * pageRows;
		SortBuilder sort = SortBuilders.fieldSort("requestTime").order(SortOrder.DESC);
		ElasticPageResult<ElasticSearchResult<ElasticLogBaseParam>> searchResult = ElasticUtils.getInstance().queryIndexAndHighlighted(elasticIndex, ElasticLogBaseParam.class, query, filter, sort, startRows, pageRows, new String[]{"logDetail"});
		List<ElasticSearchResult<ElasticLogBaseParam>> logInfos = searchResult.getList();
		PageResult<ElasticSearchResult<ElasticLogBaseParam>> result = new PageResult<ElasticSearchResult<ElasticLogBaseParam>>(page, pageRows, searchResult.getTotalSize());
		result.setList(logInfos);
		return result;
	}

	private BoolQueryBuilder getQuery(LogSearchRequest request) {
		BoolQueryBuilder query = QueryBuilders.boolQuery().must(QueryBuilders.matchPhraseQuery("_type", Constants.ELASTIC_TYPE_BASE_PARAM_TYPE));
		String[] keyword = request.getKeyword();
		
		if(keyword != null && keyword.length > 0) {
			for(String temp : keyword) {
				if (StringUtils.isNotBlank(temp)) {
					query.must(QueryBuilders.matchQuery("logDetail", keyword));
				}
			}
		}
		return query;
	}

	private BoolQueryBuilder getFilter(LogSearchRequest request) {
		BoolQueryBuilder filter = QueryBuilders.boolQuery();
		String method = request.getMethod();
		if (StringUtils.isNotBlank(method)) {
			filter.must(QueryBuilders.matchPhraseQuery("method", method));
		}
		String version = request.getVersion();
		if (StringUtils.isNotBlank(version)) {
			filter.must(QueryBuilders.matchPhraseQuery("version", version));
		}
		String threadName = request.getThreadName();
		if (StringUtils.isNotBlank(threadName)) {
			filter.must(QueryBuilders.matchPhraseQuery("threadName", threadName));
		}
		String firstChannel = request.getFirstChannel();
		if (StringUtils.isNotBlank(firstChannel)) {
			filter.must(QueryBuilders.matchPhraseQuery("firstChannel", firstChannel));
		}
		String secondChannel = request.getSecondChannel();
		if (StringUtils.isNotBlank(secondChannel)) {
			filter.must(QueryBuilders.matchPhraseQuery("secondChannel", secondChannel));
		}
		String ip = request.getIp();
		if (StringUtils.isNotBlank(ip)) {
			filter.must(QueryBuilders.matchPhraseQuery("ip", ip));
		}
		String lvversion = request.getLvversion();
		if (StringUtils.isNotBlank(lvversion)) {
			filter.must(QueryBuilders.matchPhraseQuery("lvversion", lvversion));
		}
		String lvsessionid = request.getLvsessionid();
		if (StringUtils.isNotBlank(lvsessionid)) {
			filter.must(QueryBuilders.matchPhraseQuery("lvsessionid", lvsessionid));
		}
		String startTime = request.getStartTime();
		RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("requestTime");
		if (StringUtils.isNotBlank(startTime)) {
			Date startDate = DateUtil.toDate(startTime, "yyyy-MM-dd HH:mm:ss");
			rangeQueryBuilder = rangeQueryBuilder.from(startDate.getTime());
		}
		String endTime = request.getEndTime();
		if(StringUtils.isNotBlank(endTime)) {
			Date endDate = DateUtil.toDate(endTime, "yyyy-MM-dd HH:mm:ss");
			rangeQueryBuilder = rangeQueryBuilder.to(endDate.getTime());
		}
		if(StringUtils.isNotBlank(startTime) || StringUtils.isNotBlank(startTime)) {
			filter.must(rangeQueryBuilder);
		}
		/*String latitude = request.getLatitude();
		if (StringUtils.isNotBlank(latitude)) {
			query.must(QueryBuilders.termQuery("latitude", latitude));
		}
		String longitude = request.getLongitude();
		if (StringUtils.isNotBlank(longitude)) {
			query.must(QueryBuilders.termQuery("longitude", longitude));
		}*/
		return filter;
	}
}
